package com.wwfish.cmsui.modules.resource.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.*;
import com.nexustar.gwt.dashboard.client.AbstractPage;
import com.nexustar.gwt.dashboard.client.PageClient;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.model.PaginationModelDto;
import com.nexustar.gwt.widgets.client.ui.button.MyButton;
import com.nexustar.gwt.widgets.client.ui.form.FormContainer;
import com.nexustar.gwt.widgets.client.ui.form.FormElement;
import com.nexustar.gwt.widgets.client.ui.form.FormElementProviderAdpter;
import com.nexustar.gwt.widgets.client.ui.form.IFormElementProvider;
import com.nexustar.gwt.widgets.client.ui.iprovider.IPaginationModelProvider;
import com.nexustar.gwt.widgets.client.ui.panel.ContentPanel;
import com.nexustar.gwt.widgets.client.ui.panel.InfoPanel;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.nexustar.gwt.widgets.client.ui.textbox.FishLabelListBox;
import com.nexustar.gwt.widgets.client.ui.textbox.FishLabelTextBox;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolItem;
import com.nexustar.gwt.widgets.client.ui.window.IMessageConfirmCall;
import com.nexustar.gwt.widgets.client.ui.window.Message;
import com.wwfish.cms.model.CMSConstants;
import com.wwfish.cms.model.ResourceDto;
import com.wwfish.cms.service.ResourceManager;
import com.wwfish.cmsui.modules.common.client.BaseCMSPage;
import com.wwfish.cmsui.modules.common.client.util.CodesHelper;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-7
 * Time: 9:43:43
 * To change this template use File | Settings | File Templates.
 */
public class ResourceManagePage extends BaseCMSPage {
    private PaginationTable tableList;
    private FormContainer searchForm;
    private ResourceWidget resourceWidget;

    @Override
    protected boolean isRunAsync() {
        return true;
    }

    @Override
    public void initWidgets(final DockPanel page) {
        GWT.runAsync(new RunAsyncCallback() {
            public void onFailure(Throwable reason) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void onSuccess() {
                page.setWidth("100%");
                VerticalPanel vp = new VerticalPanel();
                vp.setWidth("100%");
                vp.setHeight("100%");
                vp.add(getMenuBar());
                initTableList();
                ContentPanel tableListWrapper = new ContentPanel("资源管理列表");
                tableListWrapper.setWidth("100%");
                tableListWrapper.setHeight("100%");
                tableListWrapper.addContentWidget(tableList);
                vp.add(tableListWrapper);
                vp.setCellHeight(tableListWrapper, "96%");

                ContentPanel north = getSearchPanel();
                page.add(north, DockPanel.NORTH);
                page.setCellHeight(north, "45px");
                page.add(vp, DockPanel.CENTER);

                resourceWidget = new ResourceWidget();

                client.success(null);
            }
        });

    }

    private Widget getMenuBar() {
        ToolBar bar = new ToolBar();
        bar.setWidth("100%");
        ToolItem add = new ToolItem("添加");
        ToolItem edit = new ToolItem("编辑");

        ToolItem delete = new ToolItem("删除");
        bar.addToolItem(add);
        bar.addToolItem(edit);
        bar.addToolItem(delete);

        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                resourceWidget.show(null);
            }
        });
        edit.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (tableList.getTable().getSelectedItem() != null) {
                    resourceWidget.show((ResourceDto) tableList.getTable().getSelectedItem().getUserObject());
                }
            }
        });
        delete.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (tableList.getTable().getSelectedItem() != null) {
                    Message.confirm("确认要删除?", "温馨提示", new IMessageConfirmCall() {
                        public void doExcute(boolean flag) {
                            if (flag) {
                                ServiceFactory.invoke(ResourceManager.class.getName(), "delete", new Object[]{
                                        tableList.getTable().getSelectedItem().getUserObject()}, new FishAsyncCallback() {
                                    public void onSuccess(Object o) {
                                        InfoPanel.show("资源删除成功！");
                                        ddOutPageModel(null);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
        return bar;
    }

    private void initTableList() {
        ViewTable table = new ViewTable(getColsWaiter());
        tableList = new PaginationTable(table, 20);
        tableList.getTable().setHeight("400px");
        tableList.getModelManger().setProvider(new IPaginationModelProvider() {
            public void setInputData(int begin, int count, final IAsyncModelCallback asyn) {
                ServiceFactory.invoke(ResourceManager.class.getName(), "getPageEntities", new Object[]{begin, count,
                        searchForm.getModelManger().getModel()}, new FishAsyncCallback() {
                    public void onSuccess(Object o) {
                        asyn.setModelElments(o);
                    }
                });
            }
        });
    }

    private ContentPanel getSearchPanel() {
        ContentPanel searchPanel = new ContentPanel("查询条件");
        searchPanel.setWidth("100%");
        final FishLabelTextBox sName = new FishLabelTextBox("资源名字:");
        final FishLabelListBox type = new FishLabelListBox("类型:");
        final MyButton go = new MyButton("查询");
        CodesHelper.initGeneralCodesToListWidget(CodesHelper.getCodesById(CMSConstants._CODE_RESOURCE_VARIETY_ID), (ListBox) type.getUI(), false);
        go.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                tableList.getModelManger().renderAsyncModel(new Object[]{1, 20});
            }
        });
        searchForm = new FormContainer(3);
        searchForm.getModelManger().renderModel(new HashMap(1));
        searchForm.addElement(new FormElement(sName, new FormElementProviderAdpter() {
            public void getValue(Object object) {
                ((Map) object).put("name", sName.getValue());
            }

            public void setValue(Object object) {
                sName.setValue(((Map) object).get("name"));
            }
        }));
        searchForm.addElement(new FormElement(type, new FormElementProviderAdpter() {
            public void getValue(Object object) {
                ((Map) object).put("type", type.getValue());
            }

            public void setValue(Object object) {
                sName.setValue(((Map) object).get("type"));
            }
        }));
        searchForm.addWidget(go);
        searchPanel.addContentWidget(searchForm);

        return searchPanel;
    }

    private ColumnWaiter getColsWaiter() {

        ColumnExt co1 = new ColumnExt("资源名称", 200, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                ResourceDto resource = (ResourceDto) cell.getUserObject();
                cell.setText(resource
                        .getName());
            }
        });

        ColumnExt co3 = new ColumnExt("类型", 250, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                ResourceDto resource = (ResourceDto) cell.getUserObject();
                cell.setText(CodesHelper.getNameByCode(CMSConstants._CODE_RESOURCE_VARIETY_ID, resource.getType()));
            }
        });
        ColumnExt co4 = new ColumnExt("创建日期", 350, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                ResourceDto resource = (ResourceDto) cell.getUserObject();
                cell.setText(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").format(resource.getCreateTime()));
            }
        });
        ColumnExt co5 = new ColumnExt("操作", 60, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                MyButton button = new MyButton("预览");
                cell.setWidget(button);
                button.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent clickEvent) {
                        //todo:
                    }
                });
            }
        });
        ColumnWaiter cw = new ColumnWaiter(new ColumnExt[]{co1, co3, co4, co5});
        return cw;
    }

    @Override
    protected void ddOutPageModel(Object o) {
        tableList.getModelManger().renderAsyncModel(new Object[]{0, 20});
    }

}
